home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / cube.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  4.9 KB  |  174 lines

  1. //
  2. // "$Id: cube.cxx,v 1.4 1999/01/07 19:17:51 mike Exp $"
  3. //
  4. // Another forms test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Modified to have 2 cubes to test multiple OpenGL contexts
  7. //
  8. // Copyright 1998-1999 by Bill Spitzak and others.
  9. //
  10. // This library is free software; you can redistribute it and/or
  11. // modify it under the terms of the GNU Library General Public
  12. // License as published by the Free Software Foundation; either
  13. // version 2 of the License, or (at your option) any later version.
  14. //
  15. // This library is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. // Library General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Library General Public
  21. // License along with this library; if not, write to the Free Software
  22. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  23. // USA.
  24. //
  25. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  26. //
  27.  
  28. #include <config.h>
  29. #include <FL/Fl.H>
  30. #include <FL/Fl_Window.H>
  31. #include <FL/Fl_Box.H>
  32. #include <FL/Fl_Button.H>
  33. #include <FL/Fl_Radio_Light_Button.H>
  34. #include <FL/Fl_Slider.H>
  35. #include <stdlib.h>
  36.  
  37. #if !HAVE_GL
  38. class cube_box : public Fl_Box {
  39. public:    
  40.   double lasttime;
  41.   int wire;
  42.   double size;
  43.   double speed;
  44.   cube_box(int x,int y,int w,int h,const char *l=0)
  45.     :Fl_Box(FL_DOWN_BOX,x,y,w,h,l){
  46.       label("This demo does\nnot work without GL");
  47.   }
  48. };
  49. #else
  50. #include <FL/Fl_Gl_Window.H>
  51. #include <FL/gl.h>
  52.  
  53. class cube_box : public Fl_Gl_Window {
  54.   void draw();
  55. public:
  56.   double lasttime;
  57.   int wire;
  58.   double size;
  59.   double speed;
  60.   cube_box(int x,int y,int w,int h,const char *l=0)
  61.     : Fl_Gl_Window(x,y,w,h,l) {lasttime = 0.0;}
  62. };
  63.  
  64. /* The cube definition */
  65. float v0[3] = {0.0, 0.0, 0.0};
  66. float v1[3] = {1.0, 0.0, 0.0};
  67. float v2[3] = {1.0, 1.0, 0.0};
  68. float v3[3] = {0.0, 1.0, 0.0};
  69. float v4[3] = {0.0, 0.0, 1.0};
  70. float v5[3] = {1.0, 0.0, 1.0};
  71. float v6[3] = {1.0, 1.0, 1.0};
  72. float v7[3] = {0.0, 1.0, 1.0};
  73.  
  74. #define v3f(x) glVertex3fv(x)
  75.  
  76. void drawcube(int wire) {
  77. /* Draw a colored cube */
  78.   glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  79.   glColor3ub(0,0,255);
  80.   v3f(v0); v3f(v1); v3f(v2); v3f(v3);
  81.   glEnd();
  82.   glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  83.   glColor3ub(0,255,255); v3f(v4); v3f(v5); v3f(v6); v3f(v7);
  84.   glEnd();
  85.   glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  86.   glColor3ub(255,0,255); v3f(v0); v3f(v1); v3f(v5); v3f(v4);
  87.   glEnd();
  88.   glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  89.   glColor3ub(255,255,0); v3f(v2); v3f(v3); v3f(v7); v3f(v6);
  90.   glEnd();
  91.   glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  92.   glColor3ub(0,255,0); v3f(v0); v3f(v4); v3f(v7); v3f(v3);
  93.   glEnd();
  94.   glBegin(wire ? GL_LINE_LOOP : GL_POLYGON);
  95.   glColor3ub(255,0,0); v3f(v1); v3f(v2); v3f(v6); v3f(v5);
  96.   glEnd();
  97. }
  98.  
  99. void cube_box::draw() {
  100.   lasttime = lasttime+speed;
  101.   if (!valid()) {
  102.     glLoadIdentity();
  103.     glViewport(0,0,w(),h());
  104.     glEnable(GL_DEPTH_TEST);
  105.     glFrustum(-1,1,-1,1,2,10000);
  106.     glTranslatef(0,0,-10);
  107.   }
  108.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  109.   glPushMatrix();
  110.   glRotatef(float(lasttime*1.6),0,0,1);
  111.   glRotatef(float(lasttime*4.2),1,0,0);
  112.   glRotatef(float(lasttime*2.3),0,1,0);
  113.   glTranslatef(-1.0, 1.2f, -1.5);
  114.   glScalef(float(size),float(size),float(size));
  115.   drawcube(wire);
  116.   glPopMatrix();
  117. }
  118.  
  119. #endif
  120.  
  121. Fl_Window *form;
  122. Fl_Slider *speed, *size;
  123. Fl_Button *button, *wire, *flat;
  124. cube_box *cube, *cube2;
  125.  
  126. void makeform(const char *name) {
  127.   form = new Fl_Window(510+390,390,name);
  128.   new Fl_Box(FL_DOWN_FRAME,20,20,350,350,"");
  129.   new Fl_Box(FL_DOWN_FRAME,510,20,350,350,"");
  130.   speed = new Fl_Slider(FL_VERT_SLIDER,390,90,40,220,"Speed");
  131.   size = new Fl_Slider(FL_VERT_SLIDER,450,90,40,220,"Size");
  132.   wire = new Fl_Radio_Light_Button(390,20,100,30,"Wire");
  133.   flat = new Fl_Radio_Light_Button(390,50,100,30,"Flat");
  134.   button = new Fl_Button(390,340,100,30,"Exit");
  135.   cube = new cube_box(23,23,344,344, 0);
  136.   cube2 = new cube_box(513,23,344,344, 0);
  137.   Fl_Box *b = new Fl_Box(FL_NO_BOX,cube->x(),size->y(),
  138.              cube->w(),size->h(),0);
  139.   form->resizable(b);
  140.   b->hide();
  141.   form->end();
  142. }
  143.  
  144. main(int argc, char **argv) {
  145.   makeform(argv[0]);
  146.   speed->bounds(4,0);
  147.   speed->value(cube->speed = cube2->speed = 1.0);
  148.   size->bounds(4,0.01);
  149.   size->value(cube->size = cube2->size = 1.0);
  150.   flat->value(1); cube->wire = 0; cube2->wire = 1;
  151.   form->label("cube");
  152.   form->show(argc,argv);
  153.   cube->show();
  154.   cube2->show();
  155.   for (;;) {
  156.     if (form->visible() && speed->value())
  157.       {if (!Fl::check()) break;}    // returns immediately
  158.     else
  159.       {if (!Fl::wait()) break;}    // waits until something happens
  160.     cube->wire = wire->value();
  161.     cube2->wire = !wire->value();
  162.     cube->size = cube2->size = size->value();
  163.     cube->speed = cube2->speed = speed->value();
  164.     cube->redraw();
  165.     cube2->redraw();
  166.     if (Fl::readqueue() == button) break;
  167.   }
  168.   return 0;
  169. }
  170.  
  171. //
  172. // End of "$Id: cube.cxx,v 1.4 1999/01/07 19:17:51 mike Exp $".
  173. //
  174.